import pygame, sys, random
from pygame.locals import *

# 定义游戏参数
FPS = 30
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
BOARD_SIZE = 5  # 棋盘大小是 5 * 5
STAR_POINTS = [(170, 100), (270, 30), (370, 100), (340, 200), (220, 200)]  # 五角星顶点位置
STAR_LINES = [(170, 100), (270, 30), (370, 100), (220, 200), (340, 200), (170, 100)]  # 五角星线条路径

# 定义颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 初始化 Pygame
pygame.init()
fps_clock = pygame.time.Clock()

# 设置窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('跳棋游戏')

# 加载棋盘
board = pygame.Surface((WINDOW_WIDTH, WINDOW_HEIGHT))
board.fill(WHITE)
pygame.draw.polygon(board, BLACK, STAR_LINES)
for point in STAR_POINTS:
    pygame.draw.circle(board, BLACK, point, 10)
window.blit(board, (0, 0))

# 初始化棋子
pieces = []
radius = 20  # 棋子半径
space = 5  # 棋盘线条与棋子间隔
margin = (WINDOW_WIDTH - (BOARD_SIZE - 1) * (2*radius+space)) // 2
for row in range(BOARD_SIZE):
    for col in range(BOARD_SIZE):
        x = margin + (2*radius+space) * col
        y = margin + (2*radius+space) * row
        if (row, col) in [(2, 0), (2, 1), (2, 2), (2, 3), (2, 4),
                          (3, 1), (3, 2), (3, 3), (1, 1), (1, 2)]:
            piece = pygame.Surface((radius*2, radius*2))
            piece.set_colorkey((0, 0, 0))
            pygame.draw.circle(piece, RED, (radius, radius), radius)
            pieces.append(piece)
        else:
            pieces.append(None)

# 让玩家选择第一个被拿走的棋子
selected_piece = None
while selected_piece is None:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONUP:
            x, y = event.pos
            for i in range(len(pieces)):
                if pieces[i] is not None:
                    piece_x = margin + (2*radius+space) * (i % BOARD_SIZE)
                    piece_y = margin + (2*radius+space) * (i // BOARD_SIZE)
                    distance = ((x-piece_x-radius)**2 + (y-piece_y-radius)**2)**0.5
                    if distance <= radius:
                        selected_piece = i
                        pieces[selected_piece] = None
                        break

pygame.display.update()
